/*
 * Copyright (C) hushenglan
 */

#include "mysys.h"

namespace mysys
{

/*
 * 构造函数
 */
CWorkProcess::CWorkProcess(void *args)
    :CProcess(workProcessMain, args)
{
}

/*
 * 析构函数
 */
CWorkProcess::~CWorkProcess()
{
}

/*
 * main函数
 */
int CWorkProcess::workProcessMain(void *args)
{
    setAtexit(workProcessAtexit);

    work_process_arg_t *args_p = (work_process_arg_t *)args;
    CServerSock *serverSock = args_p->serverSock;

    CPoll *eventModel = new CEpollPoll(20, serverSock);
    eventModel.create();

    long timeout = -1;
    while(_isNeedWork)
    {
        int max = eventModel.waitForEvents(timeout);

        for(int i = 0; i < max; i++)
        {
            eventModel.getEvent();
            //TODO dispose
        }
    }

}

/*
 * 进程退出时的动作
 */
void CWorkProcess::workProcessAtexit()
{
    MS_DEBUG("process %d stop\n", getPid());
}

/*
 * 关闭进程
 */
void CWorkProcess::stop()
{
    _isNeedWork = false;
}

}
